import requests
import random
import pymysql
import time
newtime=time.strftime('%Y-%m-%d')
def ua():
    ua_pool = [

        "Mozilla/5.0 (Windows NT 6.2;en-US) AppleWebKit/537.32.36 (KHTML, live Gecko) Chrome/59.0.3047.101 Safari/537.32",

        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36",

        "Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.5(0x18000528) NetType/4G Language/zh_CN",

        "Mozilla/5.0 (Linux; Android 11; M2012K11AC Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045613 Mobile Safari/537.36 MMWEBID/7803 MicroMessenger/8.0.3.1880(0x2800033D) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64",

        "Mozilla/5.0 (Linux; Android 9; Mi Note 3 Build/PKQ1.181007.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045609 Mobile Safari/537.36 MMWEBID/4799 MicroMessenger/8.0.2.1860(0x2800023D) Process/tools WeChat/arm64 Weixin NetType/4G Language/zh_CN ABI/arm64",

        "Mozilla/5.0 (iPhone; CPU iPhone OS 14_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.5(0x18000528) NetType/4G Language/zh_CN",

        "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",

        "Mozilla/5.0 (Linux; Android 11; V1824A Build/RP1A.200720.012; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045611 Mobile Safari/537.36 MMWEBID/8305 MicroMessenger/8.0.2.1860(0x2800023D) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64",

        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",

        "Mozilla/5.0 (iPhone; CPU iPhone OS 14_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.3(0x1800032c) NetType/WIFI Language/zh_CN",

        "Mozilla/5.0 (Linux; U; Android 10; zh-cn; MI 9 Build/QKQ1.190825.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/13.3.12",
    ]
    return ua_pool[random.randint(1, 10)]


def login():
    login_url = 'http://sw.eccang.com/default/index/login'
    headers = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Length": "56",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Host": "sw.eccang.com",
        "Origin": "http://sw.eccang.com",
        "Pragma": "no-cache",
        # "Accept": "application/json, text/javascript, */*; q=0.01",
        "User-Agent": "{}".format(ua()),
        "Referer": "http://sw.eccang.com/default/index/logout?company_code=sw"
    }
    body = {
        "userName": "jinhuaitao",
        "userPass": "jht834719#",
        "companyCode": ""
    }
    try:
        res = requests.post(url=login_url, headers=headers, data=body)
        cookies = res.cookies.items()
        print(cookies)
        cookie = ''
        for name, value in cookies:
            cookie += '{0}={1};'.format(name, value)
        print('UM_distinctid=17c15b16b7275-02b55430879ff9-2343360-1fa400-17c15b16b7376d;' + cookie)
        return cookie
    except Exception as err:
        print('获取cookie失败：\n{0}'.format(err))


conn = pymysql.connect(host='14.29.212.205', port=8306, user='asinuser',
                       password='16Asin@~!1^g902', database='sunway_db', charset='utf8')
# 创建游标
cursor = conn.cursor()


def on_mysql(pp_id, pp_no, pp_transaction_no, pp_source_type, pp_offs, supplier_id, supplier_code, supplier_name,
             custom_date, pp_applicant, pp_verifier, pp_approver, operator_purchase, pts_oprater, account_type,
             fts_status_sort, pp_status, pp_amount, currency_code, pp_remark, pp_add_time, pp_verify_time,
             pp_approver_time, pp_pay_time, po_code, reexamine_time, pp_approval_note, fm_no, pp_ship_amount,
             transaction_no, po_type, pay_type, pc_pay_date, payment_cycle_type, account_proportion, ft_code,
             pp_apply_remark, po_complete_type, ref_no, pp_pay_date_eta, pp_sku_fee, pp_tax_fee, pp_ship_fee,
             po_platform, company, warehouse_id, po_platform_amount, po_remark, po_is_order, po_trade_type, date_create,
             supplier_ba_id, po_ba_id, pc_id, net_number, receiving_code, recevingTotal, qtyTotal, meno, pm_name,
             pm_bankname, examine_str, is_exception):
    sql = 'insert into eccang_payment_notices_order(`pp_id`,`pp_no`,`pp_transaction_no`,`pp_source_type`,`pp_offs`,`supplier_id`,`supplier_code`,`supplier_name`,`custom_date`,`pp_applicant`,`pp_verifier`,`pp_approver`,`operator_purchase`,`pts_oprater`,`account_type`,`fts_status_sort`,`pp_status`,`pp_amount`,`currency_code`,`pp_remark`,`pp_add_time`,`pp_verify_time`,`pp_approver_time`,`pp_pay_time`,`po_code`,`reexamine_time`,`pp_approval_note`,`fm_no`,`pp_ship_amount`,`transaction_no`,`po_type`,`pay_type`,`pc_pay_date`,`payment_cycle_type`,`account_proportion`,`ft_code`,`pp_apply_remark`,`po_complete_type`,`ref_no`,`pp_pay_date_eta`,`pp_sku_fee`,`pp_tax_fee`,`pp_ship_fee`,`po_platform`,company,`warehouse_id`,`po_platform_amount`,`po_remark`,`po_is_order`,`po_trade_type`,`date_create`,`supplier_ba_id`,`po_ba_id`,`pc_id`,`net_number`,`receiving_code`,`recevingTotal`,`qtyTotal`,`meno`,`pm_name`,`pm_bankname`,`examine_str`,`is_exception`) values(%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s, %s, %s,%s)'
    print('{}插入到数据库中'.format(po_code))
    cursor.execute(sql, (
        pp_id, pp_no, pp_transaction_no, pp_source_type, pp_offs, supplier_id, supplier_code, supplier_name,
        custom_date,
        pp_applicant, pp_verifier, pp_approver, operator_purchase, pts_oprater, account_type, fts_status_sort,
        pp_status,
        pp_amount, currency_code, pp_remark, pp_add_time, pp_verify_time, pp_approver_time, pp_pay_time, po_code,
        reexamine_time, pp_approval_note, fm_no, pp_ship_amount, transaction_no, po_type, pay_type, pc_pay_date,
        payment_cycle_type, account_proportion, ft_code, pp_apply_remark, po_complete_type, ref_no, pp_pay_date_eta,
        pp_sku_fee, pp_tax_fee, pp_ship_fee, po_platform, company, warehouse_id, po_platform_amount, po_remark,
        po_is_order,
        po_trade_type, date_create, supplier_ba_id, po_ba_id, pc_id, net_number, receiving_code, recevingTotal,
        qtyTotal,
        meno, pm_name, pm_bankname, examine_str, is_exception))
    conn.commit()
    print('{}提交数据到数据库中'.format(po_code))


headers = {
    'Cookie': '{}'.format(login()),
    "User-Agent": "{}".format(ua()),

}

data = {
    "ppIdStr": "",
    "E9": "",
    "receiving_st": "",
    "pay_type": "",
    "account_type": "",
    "finance_sort": "",
    "po_type": "",
    "is1688Purchase": "",
    "ispurchaseorder": "",
    "is_exception": "",
    "searchType": "po_code",
    "poCodeType": "1",
    "searchCode": "",
    "refNoType": "1",
    "ref_no": "",
    "pp_no": "",
    "transaction_no": "",
    "net_number": "",
    "pp_applicant": "",
    "operator_purchase": "",
    "pts_oprater": "",
    "operator_examine": "",
    "suppiler_id": "",
    "supplier_code": "",
    "warehouseId": "",
    "pc_id": "",
    "pm_bankname": "",
    "company": "",
    "dateFor": f"{newtime}",
    "dateTo": f"{newtime}",
    "payEtaDateFor": "",
    "payEtaDateTo": "",
    "purchaseCreateDateFor": "",
    "purchaseCreateDateTo": "",
    "examineDateFor": "",
    "examineDateTo": "",
    "approverDateFor": "",
    "approverDateTo": "",
    "orderBy": "",
}

# print(data)
def test1():
    url = 'http://sw.eccang.com/purchase/purchase-payment/list-for-approve/list/list/page/1/pageSize/100'
    try:
        res = requests.post(url, headers=headers, data=data).json()
        # print(res)
        total = int(res["total"])
        total1 = total % 1000
    except Exception  as g:
        res = requests.post(url, headers=headers, data=data).json()
        # print(res)
        total = int(res["total"])
        total1 = total % 1000
    if total1 != 0:
        page = total1 + 1
    else:
        page = total1

    count = 0

    for pages in range(1, page + 1):
        url = f'http://sw.eccang.com/purchase/purchase-payment/list-for-approve/list/list/page/{pages}/pageSize/1000'
        res1 = requests.post(url, headers=headers, data=data).json()
        for i in res1["data"]:
            count += 1
            pp_id = i["pp_id"]
            pp_no = i["pp_no"]
            pp_transaction_no = i["pp_transaction_no"]
            pp_source_type = i["pp_source_type"]
            pp_offs = i["pp_offs"]
            supplier_id = i["supplier_id"]
            supplier_code = i["supplier_code"]
            supplier_name = i["supplier_name"]
            custom_date = i["custom_date"]
            pp_applicant = i["pp_applicant"]
            pp_verifier = i["pp_verifier"]
            pp_approver = i["pp_approver"]
            operator_purchase = i["operator_purchase"]
            pts_oprater = i["pts_oprater"]
            account_type = i["account_type"]
            fts_status_sort = i["fts_status_sort"]
            pp_status = i["pp_status"]
            pp_amount = i["pp_amount"]
            currency_code = i["currency_code"]
            pp_remark = i["pp_remark"]
            pp_add_time = i["pp_add_time"]
            pp_verify_time = i["pp_verify_time"]
            pp_approver_time = i["pp_approver_time"]
            pp_pay_time = i["pp_pay_time"]
            po_code = i["po_code"]
            reexamine_time = i["reexamine_time"]
            pp_approval_note = i["pp_approval_note"]
            fm_no = i["fm_no"]
            pp_ship_amount = i["pp_ship_amount"]
            transaction_no = i["transaction_no"]
            po_type = i["po_type"]
            pay_type = i["pay_type"]
            pc_pay_date = i["pc_pay_date"]
            payment_cycle_type = i["payment_cycle_type"]
            account_proportion = i["account_proportion"]
            ft_code = i["ft_code"]
            pp_apply_remark = i["pp_apply_remark"]
            po_complete_type = i["po_complete_type"]
            ref_no = i["ref_no"]
            pp_pay_date_eta = i["pp_pay_date_eta"]
            pp_sku_fee = i["pp_sku_fee"]
            pp_tax_fee = i["pp_tax_fee"]
            pp_ship_fee = i["pp_ship_fee"]
            po_platform = i["po_platform"]
            company = i["company"]
            warehouse_id = i["warehouse_id"]
            po_platform_amount = i["po_platform_amount"]
            po_remark = i["po_remark"]
            po_is_order = i["po_is_order"]
            po_trade_type = i["po_trade_type"]
            date_create = i["date_create"]
            supplier_ba_id = i["supplier_ba_id"]
            po_ba_id = i["po_ba_id"]
            pc_id = i["pc_id"]
            net_number = i["net_number"]
            receiving_code = i["receiving_code"]
            recevingTotal = i["recevingTotal"]
            qtyTotal = i["qtyTotal"]
            meno = i["meno"]
            pm_name = i["pm_name"]
            pm_bankname = i["pm_bankname"]
            examine_str = i["examine_str"]
            is_exception = i["is_exception"]
            all = f'{pp_id},{pp_no},{pp_transaction_no},{pp_source_type},{pp_offs},{supplier_id},{supplier_code},{supplier_name},{custom_date},{pp_applicant},{pp_verifier},{pp_approver},{operator_purchase},{pts_oprater},{account_type},{fts_status_sort},{pp_status},{pp_amount},{currency_code},{pp_remark},{pp_add_time},{pp_verify_time},{pp_approver_time},{pp_pay_time},{po_code},{reexamine_time},{pp_approval_note},{fm_no},{pp_ship_amount},{transaction_no},{po_type},{pay_type},{pc_pay_date},{payment_cycle_type},{account_proportion},{ft_code},{pp_apply_remark},{po_complete_type},{ref_no},{pp_pay_date_eta},{pp_sku_fee},{pp_tax_fee},{pp_ship_fee},{po_platform},{company},{warehouse_id},{po_platform_amount},{po_remark},{po_is_order},{po_trade_type},{date_create},{supplier_ba_id},{po_ba_id},{pc_id},{net_number},{receiving_code},{recevingTotal},{qtyTotal},{meno},{pm_name},{pm_bankname},{examine_str},{is_exception}'
            print(f'第{count}个数据{all}')
            on_mysql(pp_id, pp_no, pp_transaction_no, pp_source_type, pp_offs, supplier_id, supplier_code,
                     supplier_name,
                     custom_date, pp_applicant, pp_verifier, pp_approver, operator_purchase, pts_oprater, account_type,
                     fts_status_sort, pp_status, pp_amount, currency_code, pp_remark, pp_add_time, pp_verify_time,
                     pp_approver_time, pp_pay_time, po_code, reexamine_time, pp_approval_note, fm_no, pp_ship_amount,
                     transaction_no, po_type, pay_type, pc_pay_date, payment_cycle_type, account_proportion, ft_code,
                     pp_apply_remark, po_complete_type, ref_no, pp_pay_date_eta, pp_sku_fee, pp_tax_fee, pp_ship_fee,
                     po_platform, company, warehouse_id, po_platform_amount, po_remark, po_is_order, po_trade_type,
                     date_create,
                     supplier_ba_id, po_ba_id, pc_id, net_number, receiving_code, recevingTotal, qtyTotal, meno,
                     pm_name,
                     pm_bankname, examine_str, is_exception)
    cursor.close()
    conn.close()


if __name__ == '__main__':
    test1()
